package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.AgentDetailInfo;
import com.ymatou.iapi.godmch.parameter.AgentInfo;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.detailagentBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.service.detailagentCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
/**
*查看代理商详情
*FreeMaker Template自动生成代码
*/
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
@TestSuite
public class Ts_detailagent{
	private static detailagentBean bean;
	private static detailagentCall call;
	DefGodMchWapper DefGodMchWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查看代理商详情");
	}

	@Before
	public void caseUp() {
		bean=new detailagentBean();
		call=new detailagentCall();
		DefGodMchWapper=new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void VerifyDb(CreateCodeBean ccbean) throws InterruptedException {
		AgentDetailInfo agentInfo = call.getResult();
		Logger.verifyEquals(1,DefGodMchWapper.selectMchRelation(ccbean.getMchId(), agentInfo.getAgentId(), agentInfo.getAgentType()).size(),"验证商户和代理关系正确");
		agent_type atDbInfo = DefGodMchWapper.selectAgentType(ccbean.getMchId(), ccbean.getRoleType()).get(0);
		Logger.verifyEquals(atDbInfo.getAgentType(), agentInfo.getAgentType(), "验证AgentType");
		Logger.verifyEquals(atDbInfo.getTypeTitle(), agentInfo.getAgentTypeTitle(), "验证AgentTypeTitle");
		Logger.verifyEquals("title", agentInfo.getAgentName(), "验证AgentName");
		Logger.verifyEquals( "remark", agentInfo.getRemark(), "验证Remark");
		Logger.verifyEquals(DGUserCallService.SellerDefImg, agentInfo.getAvatar(), "验证Avatar");
		Logger.verifyNotNull( agentInfo.getBindTime(),"验证BindTime");
		Logger.verifyEquals( "wechat",agentInfo.getWechat(),"验证wechat");
		
		//AgentModel
		Integer agentModel=ccbean.getAgentModel()==null?1:ccbean.getAgentModel();
		Logger.verifyEquals(agentModel, agentInfo.getAgentModel(), "验证AgentModel");
		//agentModelText
		if(agentModel==1)
			Logger.verifyEquals("自主选货", agentInfo.getAgentModelText(), "验证AgentModelText");
		else if(agentModel==2)
			Logger.verifyEquals("自动继承", agentInfo.getAgentModelText(), "验证AgentModelText");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailagent_001() {
		Logger.start(true,"查看代理商详情,验证商户id和代理Id同时存在-返回正常");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid, "remark");
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			bean.setMchId(ccbean.getMchId());
			bean.setAgentId(agentid);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(ccbean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailagent_001_1() {
		Logger.start(true,"查看代理商详情,验证商户id和代理Id同时存在-代理模式 1自主选货-返回正常");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(1);//1自主选货
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid, "remark");
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			bean.setMchId(ccbean.getMchId());
			bean.setAgentId(agentid);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(ccbean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_detailagent_001_2() {
		Logger.start(true,"查看代理商详情,验证商户id和代理Id同时存在-代理模式 2.自动继承-返回正常(20171015修改 继承型代理目前仅支持中国大陆商户)");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);//2.自动继承
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid, "remark");
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			bean.setMchId(ccbean.getMchId());
			bean.setAgentId(agentid);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(ccbean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailagent_002() {
		Logger.start(true,"查看代理商详情,验证商户id存在,代理Id不存在,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid, "remark");
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			bean.setMchId(ccbean.getMchId());
			bean.setAgentId(agentid+1);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证Result");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailagent_003() {
		Logger.start(true,"查看代理商详情,验证商户id不存在,,代理Id存在,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid, "remark");
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			bean.setMchId(ccbean.getMchId()+1);
			bean.setAgentId(agentid);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证Result");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailagent_004() {
		Logger.start(true,"查看代理商详情,验证商户id和代理Id同时不存在,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid, "remark");
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			bean.setMchId(ccbean.getMchId()+1);
			bean.setAgentId(agentid+1);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			Logger.verifyIsNull(call.getResult(), "验证Result");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailagent_005() {
		Logger.start(true,"查看代理商详情,验证商户id为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid, "remark");
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
//			bean.setMchId(ccbean.getMchId()+1);
			bean.setAgentId(agentid);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailagent_006() {
		Logger.start(true,"查看代理商详情,验证代理Id为空,返回错误");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid, "remark");
			GodmchManageCallService.editMchInfo(agentid, "title",null,"wechat");
			bean.setMchId(ccbean.getMchId());
//			bean.setAgentId(agentid);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("4001", call.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_detailagent_007() {
		Logger.start(true,"查看代理商详情-vip渠道多级,验证上级代理id和代理Id同时存在-返回正常");
		try {
			CreateCodeBean ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			GodmchManageCallService.SetVip(ccbean.getMchId());
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid = GodmchManageCallService.CreateSellerMch(code);
			
			ccbean.setMchId(agentid);
			GodmchManageCallService.CreateagentCall(agentid, "vipAgent");
			GodmchManageCallService.editMchInfo(agentid, "title", DGUserCallService.DefImg2, "wc");
			Integer agentType = DefGodMchWapper.selectAgentType(agentid, null).get(0).getAgentType();
			ccbean.setRoleType(agentType);
			String code2 = GodmchManageCallService.createCodeCall(ccbean).getCode();
			Integer agentid2 = GodmchManageCallService.CreateSellerMch(code2);
			
			GodmchManageCallService.EditAgentRemark(ccbean.getMchId(), agentid2, "remark");
			GodmchManageCallService.editMchInfo(agentid2, "title",null,"wechat");
			bean.setMchId(agentid);
			bean.setAgentId(agentid2);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("200", call.getString("status"), "验证status");
			VerifyDb(ccbean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}